/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var aeRequest;
var action;
var ajxUrl= "";
var CateArr = new Array();
var id;
var paymentDetailId;
var startDate;
var endDate;
var balance;
var status;
var userid;

function createXmlHttpRequestObject() {
    var ro;
    var browser = navigator.appName;
    // Need to determine IE7 and not do this.
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function createResponseXML(textXML) {
    // code for IE
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(textXML);
        return xmlDoc;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(textXML, "text/xml");
        return xmlDoc;
    }
}

function updatePaymentDetail(paymentDetailId, startDate, endDate, status, balance, userid, action){
    var url = '/RadioCabsProject-war/PaymentDetailsServlet?payDetailsId=' +paymentDetailId+ '&payStartDate=' +startDate+
    '&payEndDate=' +endDate+ '&payMoney=' + balance + '&payStatus=' + status + '&userid=' + userid +
    '&action=' + action;
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function finishUpdate(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            var response = createResponseXML(aeRequest.responseText);
            var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
            if (result == "success") {
                alert("update/edit successful");
            }else{
                alert("update/edit failed");
            }
        }
    }
}

function setEditFormPosition(ele){
    var pos = jQuery("#"+ele).position();
    jQuery("#editContainer").css({
        position : "absolute",
        top : pos.top + 18 + "px",
        left : pos.left - 885 + "px"
    });
}

function setDeleteFormPosition(){
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = jQuery("#deleteContainer").height();
    var popupWidth = jQuery("#deleteContainer").width();

    jQuery("#deleteContainer").css({
        "position" : "absolute",
        "top" : (windowHeight/2 - popupHeight/2)-50,
        "left" : (windowWidth/2 - popupWidth/2)+150
    });
}
function randomString() {
    var chars = "0123456789";
    var string_length = 6;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    return randomstring;
}

function getStartDate(){
    var d=new Date();
    var dat=d.getDate();
    var mon=d.getMonth()+1;
    var year=d.getFullYear();
    var todayDate = dat+"/"+mon+"/"+year;
    return todayDate;
}

function getEndDateByMonth(){
    var d=new Date();
    var dat=d.getDate();
    var mon=d.getMonth()+2;
    var year=d.getFullYear();
    var monthDate = dat+"/"+mon+"/"+year;
    return monthDate;
}

function getEndDateByQuaterly(){
    var d=new Date();
    var dat=d.getDate();
    var mon=d.getMonth()+4;
    var year=d.getFullYear();
    var quarterDate = dat+"/"+mon+"/"+year;
    return quarterDate;
}

String.prototype.trim = function()
{
    var trimmed = this.replace(/^\s+|\s+$/g, '') ;
    return trimmed;
};
jQuery(document).ready(function() {
    jQuery("#txtStartDate").datepicker({
        dateFormat: "dd/mm/yy"
    });
    jQuery("#txtEndDate").datepicker({
        dateFormat: "dd/mm/yy"
    });
    //jQuery("#txtBalance").datepicker();

    jQuery("#btnAdd").live('click',function(){

        });

    jQuery(".editForm").live('click',function(){
        action = "edit";
        id = this.id;
        paymentDetailId = jQuery(this).parent().parent().find('td:eq(0)').html();
        startDate = jQuery(this).parent().parent().find('td:eq(1)').html();
        endDate = jQuery(this).parent().parent().find('td:eq(2)').html();
        balance = jQuery(this).parent().parent().find('td:eq(3)').html().substring(0,2);
        status = jQuery(this).parent().parent().find('td:eq(4)').html();
        //email = jQuery(this).parent().parent().find('td:eq(5)').html();
        userid = jQuery(this).parent().parent().find('td:eq(6)').html();
        rowId = jQuery(this).parent().parent().attr('id');
        /*if(balance == "15" || balance =="10"){
            startDate = getStartDate();
            endDate = getEndDateByMonth();
        }else if(balance == "25" || balance=="45"){
            startDate = getStartDate();
            endDate = getEndDateByQuaterly();
        }*/
        status = "true";
        action = "edit";
        //updatePaymentDetail(paymentDetailId, startDate, endDate, status, balance, userid, action);

        if(jQuery("#editContainer").is(":hidden")){
            setEditFormPosition(id);
            jQuery("#editContainer").slideDown("slow");

            jQuery("#txtStartDate").val(startDate);
            jQuery("#txtEndDate").val(endDate);
            jQuery("#txtBalance").val(balance);
        }else{
            jQuery("#editContainer").slideUp("slow");
        }
    });

    jQuery("#saveEdit").live('click',function(){
        startDate = jQuery("#txtStartDate").val();
        endDate = jQuery("#txtEndDate").val();
        balance = jQuery("#txtBalance").val();

        jQuery("#"+rowId).find('td:eq(1)').html(startDate);
        jQuery("#"+rowId).find('td:eq(2)').html(endDate);
        jQuery("#"+rowId).find('td:eq(3)').html(balance + ' $');
        jQuery("#"+rowId).find('td:eq(4)').html("true");
        updatePaymentDetail(paymentDetailId, startDate, endDate, status, balance, userid, action);

        jQuery("#editContainer").slideUp("slow");
    });

    jQuery(".delForm").live('click',function(){
        id = this.id;
        paymentDetailId = jQuery(this).parent().parent().find('td:eq(0)').html();
        startDate = jQuery(this).parent().parent().find('td:eq(1)').html();
        endDate = jQuery(this).parent().parent().find('td:eq(2)').html();
        balance = jQuery(this).parent().parent().find('td:eq(3)').html().substring(0,2);
        status = jQuery(this).parent().parent().find('td:eq(4)').html();
        userid = jQuery(this).parent().parent().find('td:eq(6)').html();
        rowId = jQuery(this).parent().parent().attr('id');
        status = "false";
        if(jQuery("#deleteContainer").is(":hidden")){
            setDeleteFormPosition();
            jQuery("#deleteContainer").slideDown("slow");
        }else{
            jQuery("#deleteContainer").slideUp("slow");
        }
    });

    jQuery("#noDel").live('click',function(){
        jQuery("#deleteContainer").hide("explode",1000);
    });

    jQuery("#yesDel").live('click',function(){
        id = this.id;
        jQuery("#"+rowId).find('td:eq(1)').html("");
        jQuery("#"+rowId).find('td:eq(2)').html("");
        jQuery("#"+rowId).find('td:eq(4)').html("false");
        updatePaymentDetail(paymentDetailId, startDate, endDate, status, balance, userid, "delete");
        jQuery("#deleteContainer").hide("explode",1000);
    });
});


